<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>开发工具管理面板</title>
  </head>
  <body>
    <dl>
      <dt>MySQL<span id="statusTextDom">{{statusTextMySQL}}</span></dt>
      <dd><button onclick="MySQLAction('start')">开启</button></dd>
      <dd><button onclick="MySQLAction('stop')">关闭</button></dd>
    </dl>
    <script>
      async function MySQLAction(type) {
        try {
          const { statusText } = await fetch("/mysql", {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ type }),
          }).then((res) => res.json());
          statusTextDom.textContent = statusText;
        } catch (err) {
          console.error("提交状态失败", err);
        }
      }

      // 这里增加获取状态接口
      async function fetchMySQLStatus() {
        try {
          const res = await fetch("/mysql/status");
          const { statusText } = await res.json();
          statusTextDom.textContent = statusText;
        } catch (err) {
          console.error("获取状态失败", err);
        }
      }

      // -----------基于 requestAnimationFrame，每 3 秒执行一次------------
      const interval = 5000; // 3秒
      let lastTime = performance.now();

      (function statusSync(now) {
        if (now - lastTime >= interval) {
          lastTime = now;
          fetchMySQLStatus();
        }
        requestAnimationFrame(statusSync);
      })(performance.now());
    </script>
  </body>
</html>
